package com.kt.thread.demo;

import java.util.concurrent.*;

/**
 * Created by tangwy on 2016/1/14.
 */
public class ThreadPool {
    //不定数量的线程池
    private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    //只有一个线程的线程池
    private static ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
    //固定线程数量的线程池
    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    private static ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);

    private void print(int k, String methodName) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(methodName + ",ThreadName=" + Thread.currentThread().getName() + ",第" + k + "个任务,i=" + i);
        }
    }

    /**
     * 因为cachedThreadPool是不定数量的线程池，所以有几个任务就创建几个线程,
     * 那么这里会创建10个线程
     */
    public void testCachedThreadPool() {
        //创建10个任务
        for (int i = 1; i <= 10; i++) {
            final int task = i;
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    print(task, "testCachedThreadPool");
                }
            });
        }
    }

    /**
     * 因为SingleThreadPool只有一个线程，所以这些任务只有一个线程在执行，
     * 只有当一个任务执行完后，再执行下一个
     */
    public void testSingleThreadPool() {
        //创建10个任务
        for (int i = 1; i <= 10; i++) {
            final int task = i;
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    print(task, "testSingleThreadPool");
                }
            });
        }
    }

    public static void submit(ExecutorService executorService, Runnable task) {
        executorService.submit(task);
    }

    public static <T> T submit(ExecutorService executorService, Runnable task, T result) {
        executorService.submit(task, result);
        return result;
    }

    public static <T> T submit(ExecutorService executorService, Callable<T> task) {
        Future<T> future = executorService.submit(task);
        try {
            return future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T submit(ScheduledExecutorService ses, Callable<T> task) {
        //延迟10秒钟后执行
        Future<T> future = ses.schedule(task, 10L, TimeUnit.SECONDS);
        try {
            return future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void test() {
        Integer result = submit(ses, new CallableTest());
        System.out.println("result:" + result);
        System.out.println("====================================");
        result = submit(singleThreadPool, new Runnable() {
            @Override
            public void run() {
                System.out.println("Runnable");
            }
        }, 5);
        System.out.println("result:" + result);
    }
}
